//
//  MapManager.m
//  PigMarket
//
//  Created by mac on 2019/5/20.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "LocationManager.h"
#import <CoreLocation/CoreLocation.h>


@interface LocationManager ()<CLLocationManagerDelegate>
/**
 CLLocationManager
 */
@property (nonatomic , strong) CLLocationManager *locManager;
/**
 isFirstLocation
 */
@property (nonatomic , assign) BOOL isFirstLocation;
@end

static LocationManager *instance = nil;

@implementation LocationManager

+ (instancetype)sharedManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[LocationManager alloc] init];
    });
    return instance;
}
- (void)startLocationCompleteBlock:(void (^)(double, double))completeBlock {
    self.isFirstLocation = YES;
    if ([CLLocationManager locationServicesEnabled]) { //定位是否允许
        self.locManager = [[CLLocationManager alloc] init];
        self.locManager.delegate = self;
        self.locManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locManager.distanceFilter = 10.0f;
        [self.locManager requestWhenInUseAuthorization];
        [self.locManager startUpdatingLocation];
        self.locationResultBlock = completeBlock;

    } else {
        completeBlock(0,0);
//        [[AlertAction sharedInstance] showAlertWithTitle:nil message:@"无法获取位置信息" confirmBtnTitle:@"确定" confirmBlock:nil cancelBlock:nil];
    }

}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    if (self.isFirstLocation) {
        CLLocation *currentLocation = [locations lastObject];
        NSLog(@"经度=%f 纬度=%f 高度=%f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude, currentLocation.altitude);
        if (self.locationResultBlock) {
            self.isFirstLocation = NO;
            self.locationResultBlock(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
        }
    }

    /**反向地理编码
    CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        for (CLPlacemark *placemark in placemarks) {
            NSDictionary *addresssDic = [placemark addressDictionary];
            NSLog(@"%@", [addresssDic objectForKey:@"Country"]);
            
            NSLog(@"%@", [addresssDic objectForKey:@"State"]);
            
            NSLog(@"%@", [addresssDic objectForKey:@"City"]);
        }
    }];
     */
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    
    if ([error code] == kCLErrorDenied){
        if (self.locationResultBlock) {
            self.locationResultBlock(0,0);
        }
        //访问被拒绝
//        [[AlertAction sharedInstance] showAlertWithTitle:nil message:@"无法获取位置信息" confirmBtnTitle:@"确定" confirmBlock:nil cancelBlock:nil];

    }
    if ([error code] == kCLErrorLocationUnknown) {
        //无法获取位置信息
//        [[AlertAction sharedInstance] showAlertWithTitle:nil message:@"无法获取位置信息" confirmBtnTitle:@"确定" confirmBlock:nil cancelBlock:nil];
        if (self.locationResultBlock) {
            self.locationResultBlock(0,0);
        }
    }
}
@end
